package lium.lbs.lbsim.chat;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import lium.lbs.lbsim.R;
import lium.lbs.lbsim.chat.activity.LMMessage;
import lium.lbs.lbsim.chat.activity.MsgBody;
import lium.lbs.lbsim.chat.activity.MsgSendStatus;
import lium.lbs.lbsim.chat.activity.MsgType;
import lium.lbs.lbsim.chat.bean.*;
import lium.lbs.lbsim.chat.view.BubbleImageView;

import java.io.File;
import java.util.List;

public class ChatAdapter extends BaseQuickAdapter<LMMessage,BaseViewHolder> {

    private static final int TYPE_SEND_TEXT=1;
    private static final int TYPE_RECEIVE_TEXT=2;
    private static final int TYPE_SEND_IMAGE=3;
    private static final int TYPE_RECEIVE_IMAGE=4;
    private static final int TYPE_SEND_VIDEO=5;
    private static final int TYPE_RECEIVE_VIDEO=6;
    private static final int TYPE_SEND_FILE=7;
    private static final int TYPE_RECEIVE_FILE=8;
    private static final int TYPE_SEND_AUDIO=9;
    private static final int TYPE_RECEIVE_AUDIO=10;

    private static final int SEND_TEXT = R.layout.msg_item_text_send;
    private static final int RECEIVE_TEXT = R.layout.msg_item_text_receive;
    private static final int SEND_IMAGE = R.layout.msg_item_image_send;
    private static final int RECEIVE_IMAGE = R.layout.msg_item_image_receive;
    private static final int SEND_VIDEO = R.layout.msg_item_video_send;
    private static final int RECEIVE_VIDEO = R.layout.msg_item_video_receive;
   private static final int RECEIVE_AUDIO = R.layout.msg_item_audio_receive;
    private static final int SEND_AUDIO = R.layout.msg_item_audio_send;

    public ChatAdapter(Context context, List<LMMessage> data) {
        super(data);
        setMultiTypeDelegate(new MultiTypeDelegate<LMMessage>() {
            @Override
            protected int getItemType(LMMessage entity) {
                //根据你的实体类来判断布局类型
          boolean isSend = entity.getSenderId().equals(ChatActivity.Companion.getMSenderId());


                 if (MsgType.TEXT==entity.getMsgType()) {
                    return isSend ? TYPE_SEND_TEXT : TYPE_RECEIVE_TEXT;
                }else if(MsgType.IMAGE==entity.getMsgType()){
                     return isSend ? TYPE_SEND_IMAGE : TYPE_RECEIVE_IMAGE;
                }else if(MsgType.VIDEO==entity.getMsgType()){
                     return isSend ? TYPE_SEND_VIDEO : TYPE_RECEIVE_VIDEO;
                 }else if(MsgType.FILE==entity.getMsgType()){
                     return isSend ? TYPE_SEND_FILE : TYPE_RECEIVE_FILE;
                 }else if(MsgType.AUDIO==entity.getMsgType()){
                     return isSend ? TYPE_SEND_AUDIO : TYPE_RECEIVE_AUDIO;
                 }
                return 0;
            }
        });
        //Step.2
        getMultiTypeDelegate() .registerItemType(TYPE_SEND_TEXT, SEND_TEXT)
                .registerItemType(TYPE_RECEIVE_TEXT,RECEIVE_TEXT)
                .registerItemType(TYPE_SEND_IMAGE, SEND_IMAGE)
                .registerItemType(TYPE_RECEIVE_IMAGE, RECEIVE_IMAGE)
                .registerItemType(TYPE_SEND_VIDEO, SEND_VIDEO)
                .registerItemType(TYPE_RECEIVE_VIDEO, RECEIVE_VIDEO)
                .registerItemType(TYPE_SEND_AUDIO, SEND_AUDIO)
                .registerItemType(TYPE_RECEIVE_AUDIO, RECEIVE_AUDIO);


        //  .registerItemType(0, R.layout.LMMessage_item_chat_accept);

    }

    @Override
    protected void convert(BaseViewHolder helper, LMMessage item) {
        setContent(helper, item);
        setStatus(helper, item);
        MsgBody msgContent = item.getBody();
        if (msgContent instanceof AudioMsgBody){
            helper.addOnClickListener(R.id.rlAudio);
        //    helper.getView(R.id.ivAudio).setId(helper.getAdapterPosition());
           /* if (helper.getAdapterPosition() == -1) {
                helper.getView(R.id.ivAudio)
                        .setBackgroundResource(R.mipmap.audio_animation_list_right_3);
                helper.getView(R.id.ivAudio)
                        .setBackgroundResource(R.drawable.audio_animation_right_list);
                AnimationDrawable  drawable = (AnimationDrawable)helper.getView(R.id.ivAudio)
                        .getBackground();
                drawable.start();
            } else {
                helper.getView(R.id.ivAudio)
                        .setBackgroundResource(R.mipmap.audio_animation_list_right_3);
            }*/


        }



    }


    private void setStatus(BaseViewHolder helper, LMMessage item) {
        MsgBody msgContent = item.getBody();
        if (msgContent instanceof TextMsgBody
                || msgContent instanceof AudioMsgBody ||msgContent instanceof VideoMsgBody ||msgContent instanceof FileMsgBody) {
            //只需要设置自己发送的状态
            MsgSendStatus sentStatus = item.getSentStatus();
            boolean isSend = item.getSenderId().equals(ChatActivity.Companion.getMSenderId());
            if (isSend){
                if (sentStatus == MsgSendStatus.SENDING) {
                    helper.setVisible(R.id.chat_item_progress, true).setVisible(R.id.chat_item_fail, false);
                } else if (sentStatus == MsgSendStatus.FAILED) {
                    helper.setVisible(R.id.chat_item_progress, false).setVisible(R.id.chat_item_fail, true);
                } else if (sentStatus == MsgSendStatus.SENT) {
                    helper.setVisible(R.id.chat_item_progress, false).setVisible(R.id.chat_item_fail, false);
                }
            }
        } else if (msgContent instanceof ImageMsgBody) {
            ImageMsgBody imageLMMessage = (ImageMsgBody) msgContent;
            BubbleImageView bivPic = helper.getView(R.id.bivPic);
            boolean isSend = item.getSenderId().equals(ChatActivity.Companion.getMSenderId());
            if (isSend) {
                MsgSendStatus sentStatus = item.getSentStatus();
                if (sentStatus == MsgSendStatus.SENDING) {
                   /* bivPic.setProgressVisible(true);
                    if (!TextUtils.isEmpty(item.getExtra()))
                        bivPic.setPercent(Integer.valueOf(item.getExtra()));
                    bivPic.showShadow(true);*/
                    helper.setVisible(R.id.chat_item_progress, false).setVisible(R.id.chat_item_fail, false);
                } else if (sentStatus == MsgSendStatus.FAILED) {
                 /*   bivPic.setProgressVisible(false);
                    bivPic.showShadow(false);*/
                    helper.setVisible(R.id.chat_item_progress, false).setVisible(R.id.chat_item_fail, true);

                } else if (sentStatus == MsgSendStatus.SENT) {
                    /*bivPic.setProgressVisible(false);
                    bivPic.showShadow(false);*/
                    helper.setVisible(R.id.chat_item_progress, false).setVisible(R.id.chat_item_fail, false);
                }
            } else {
                /* bivPic.setProgressVisible(false);
                 bivPic.showShadow(false);*/
               //  helper.setVisible(R.id.chat_item_progress, false).setVisible(R.id.chat_item_fail, false);
            }
        }


    }


        private void setContent(BaseViewHolder helper, LMMessage item) {
        if (item.getMsgType().equals(MsgType.TEXT)){
           TextMsgBody msgBody = (TextMsgBody) item.getBody();
           helper.setText(R.id.chat_item_content_text, msgBody.getMessage() );
          // helper.setText(R.id,msg_item_tv_time, DateUtil.getTimeByCurrentMills(item.getSentTime()+"") );
        }else if(item.getMsgType().equals(MsgType.IMAGE)){
               ImageMsgBody msgBody = (ImageMsgBody) item.getBody();
               if (TextUtils.isEmpty(msgBody.getThumbPath() )){
                   GlideUtils.loadChatImage(mContext,msgBody.getThumbUrl(),(ImageView) helper.getView(R.id.bivPic));
                }else{
                    File file = new File(msgBody.getThumbPath());
                    if (file.exists()) {
                        GlideUtils.loadChatImage(mContext,msgBody.getThumbPath(),(ImageView) helper.getView(R.id.bivPic));
                    }else {
                        GlideUtils.loadChatImage(mContext,msgBody.getThumbUrl(),(ImageView) helper.getView(R.id.bivPic));
                    }
                }
        }else if(item.getMsgType().equals(MsgType.VIDEO)){
            VideoMsgBody msgBody = (VideoMsgBody) item.getBody();
            File file = new File(msgBody.getExtra());
            if (file.exists()) {
                GlideUtils.loadChatImage(mContext,msgBody.getExtra(),(ImageView) helper.getView(R.id.bivPic));
            }else {
                GlideUtils.loadChatImage(mContext,msgBody.getExtra(),(ImageView) helper.getView(R.id.bivPic));
            }
           /* VideoMsgBody msgBody = (VideoMsgBody) item.getBody();
            if (fileLMMessage.getLocalPath() != null && new File(fileLMMessage.getLocalPath().getPath()).exists()) {
                VideoThumbLoader.getInstance().showThumb(fileLMMessage.getLocalPath().getPath(), bivPic, 200, 200);
            } else {
                bivPic.setImageResource(R.mipmap.img_video_default);
            }*/
        }else if(item.getMsgType().equals(MsgType.FILE)){
//            FileMsgBody msgBody = (FileMsgBody) item.getBody();
//            helper.setText(R.id.msg_tv_file_name, msgBody.getDisplayName() );
//            helper.setText(R.id.msg_tv_file_size, msgBody.getSize()+"B" );
        }else if(item.getMsgType().equals(MsgType.AUDIO)){
            AudioMsgBody msgBody = (AudioMsgBody) item.getBody();
            helper.setText(R.id.tvDuration, msgBody.getDuration()+"\"" );
        }
    }
}
